using System;
using System.Data;
using System.Windows.Forms;
using TUnit.Data;
using TUnit.Class;
namespace viclip
{
    ///<summary>
    ///Date init:  
    ///Date modify:
    ///By:
    ///
    ///</summary>
    public class CGameLevels : Class
    {
        #region Declares
        int _gameLevelId = -1;
        string _gameLevelName = "";
        int _gameLevelIndex = -1;
        #endregion

        #region Constructors
        public CGameLevels()
        {
        }
        public CGameLevels(CLogin Login)
        {
            base.Login = Login;
        }
        public CGameLevels(CLogin Login, object ObjId)
        {
            base.Login = Login;
            using (AccDBGameLevels db = new AccDBGameLevels())
            {
                this.Populate((CGameLevels)db.GetClass(Login, ObjId));
            }
        }
        public CGameLevels(CLogin Login, IProvider p, System.Data.DataRow dr)
        {
            base.Login = Login;
            this.Populate(p, dr);
        }
        public CGameLevels(CLogin Login, IProvider p, object ObjId)
        {
            base.Login = Login;
            using (AccDBGameLevels db = new AccDBGameLevels())
            {
                this.Populate((CGameLevels)db.GetClass(Login, p, ObjId));
            }
        }
        #endregion

        #region Methods
        public void Populate(IProvider p, DataRow dr)
        {
            if (dr != null)
            {
                try
                {
                    if (dr.Table.Columns.IndexOf("gameLevelId") >= 0)
                    {
                        this._gameLevelId = (dr["gameLevelId"] != DBNull.Value ? Convert.ToInt32(dr["gameLevelId"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblGameLevels ADD gameLevelId [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("gameLevelName") >= 0)
                    {
                        this._gameLevelName = (dr["gameLevelName"] != DBNull.Value ? Convert.ToString(dr["gameLevelName"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblGameLevels ADD gameLevelName [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("gameLevelIndex") >= 0)
                    {
                        this._gameLevelIndex = (dr["gameLevelIndex"] != DBNull.Value ? Convert.ToInt32(dr["gameLevelIndex"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblGameLevels ADD gameLevelIndex [int] NULL");
                    //}
                }
                catch { }
            }
        }
        public void Populate(CGameLevels Obj)
        {
            if (Obj != null)
            {
                try
                {
                    this._gameLevelId = Obj.gameLevelId;
                    this._gameLevelName = Obj.gameLevelName;
                    this._gameLevelIndex = Obj.gameLevelIndex;
                }
                catch { }
            }
        }
        public override bool Save()
        {
            using (IProvider p = new OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBGameLevels db = new AccDBGameLevels())
                    {
                        if (this.EventSaving != null)
                            this.EventSaving(this);
                        if (db.Save(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaved != null)
                                this.EventSaved(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaveError != null)
                                this.EventSaveError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Save(IProvider p)
        {
            using (AccDBGameLevels db = new AccDBGameLevels())
            {
                if (db.Save(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public override bool Delete()
        {
            using (IProvider p = new OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBGameLevels db = new AccDBGameLevels())
                    {
                        if (this.EventDeleting != null)
                            this.EventDeleting(this);
                        if (db.Delete(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleted != null)
                                this.EventDeleted(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleteError != null)
                                this.EventDeleteError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Delete(IProvider p)
        {
            using (AccDBGameLevels db = new AccDBGameLevels())
            {
                if (db.Delete(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public override Form PrintPreview()
        {
            throw (new NotSupportedException("Phuong thuc nay hien chua duoc cai dat!"));
        }
        public override bool Print()
        {
            throw (new NotSupportedException("Phuong thuc nay hien chua duoc cai dat!"));
        }
        #endregion

        #region Events
        public delegate void OnEventSaveError(CGameLevels obj);
        public event OnEventSaveError EventSaveError;
        public delegate void OnEventSaving(CGameLevels GameLevels);
        public event OnEventSaving EventSaving;
        public delegate void OnEventSaved(CGameLevels GameLevels);
        public event OnEventSaved EventSaved;
        public delegate void OnEventDeleting(CGameLevels GameLevels);
        public event OnEventSaving EventDeleting;
        public delegate void OnEventDeleted(CGameLevels GameLevels);
        public event OnEventDeleted EventDeleted;
        public delegate void OnEventDeleteError(CGameLevels obj);
        public event OnEventDeleteError EventDeleteError;
        #endregion

        #region Properties
        public int gameLevelId
        {
            get { return this._gameLevelId; }
            set
            {
                if (this._gameLevelId != value)
                {
                    this._gameLevelId = value;
                }
            }
        }
        public string gameLevelName
        {
            get { return this._gameLevelName; }
            set
            {
                if (this._gameLevelName != value)
                {
                    this._gameLevelName = value;
                }
            }
        }
        public int gameLevelIndex
        {
            get { return this._gameLevelIndex; }
            set
            {
                if (this._gameLevelIndex != value)
                {
                    this._gameLevelIndex = value;
                }
            }
        }
        #endregion
    }

    #region Class AccDB
    public class AccDBGameLevels : AccDB
    {
        /// <summary>
        /// 
        /// </summary>
        public AccDBGameLevels()
        {
        }
        /// <summary>
        ///Phuong thuc dung dien du lieu len Combo 
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="cmb"></param>
        /// <param name="extName"></param>
        public override void FillComboBox(CLogin Login, ComboBox cmb, string extName)
        {
            IProvider p = new OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT GameLevelsId, GameLevelsName";
                sql += " FROM  tblGameLevels";
                sql += " WHERE (GameLevelsId >= 0)";
                sql += " ORDER BY GameLevelsName";
                DataTable table = p.OpenTable(sql, "tblGameLevels");
                if (table != null)
                {
                    if (extName != "")
                    {
                        System.Data.DataRow row = table.NewRow();
                        row["GameLevelsName"] = extName;
                        row["GameLevelsId"] = "-1";
                        table.Rows.Add(row);
                    }
                    cmb.DataSource = table;
                    cmb.DisplayMember = "GameLevelsName";
                    cmb.ValueMember = "GameLevelsId";
                    p.Close();
                }
            }
        }
        /// <summary>
        ///Doc doi tuong tu CSDL theo ID chi dinh
        /// </summary>
        /// <param name="p"></param>
        /// <param name="ObjId"></param>
        /// <returns></returns>
        public override Class GetClass(CLogin Login, IProvider p, object ObjId)
        {
            try
            {
                string sql = "SELECT * FROM tblGameLevels WHERE (gameLevelId = " + ObjId + ")";
                DataTable table = p.OpenTable(sql, "tblGameLevels");
                if ((table != null) && (table.Rows.Count > 0))
                {
                    return new CGameLevels(Login, p, table.Rows[0]);
                }
            }
            catch
            {
            }
            return null;
        }
        /// <summary>
        /// Luu doi tuong vao CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Save(IProvider p, Class obj)
        {
            CGameLevels GameLevels = (CGameLevels)obj;
            string sql = "SELECT * FROM tblGameLevels";
            sql += " WHERE GameLevelsId =" + GameLevels.gameLevelId + "";
            DataTable dt = p.OpenTable(sql, "tblGameLevels");
            if (dt != null)
            {
                DataRow row = null;
                if (dt.Rows.Count == 1 && GameLevels.gameLevelId != -1)
                {
                    row = dt.Rows[0];
                }
                else
                {
                    GameLevels.gameLevelId = Convert.ToInt32(p.InitIDKey("GameLevelsId", "tblGameLevels", null));
                    row = dt.NewRow();
                    dt.Rows.Add(row);
                }
                row["gameLevelId"] = GameLevels.gameLevelId;
                row["gameLevelName"] = GameLevels.gameLevelName;
                row["gameLevelIndex"] = GameLevels.gameLevelIndex;
                if (!p.UpdateTable("tblGameLevels", dt))
                {
                    return false;
                }
                return true;
            }
            return false;
        }
        /// <summary>
        ///Xoa doi tuong khoi CSDL 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Delete(IProvider p, Class obj)
        {
            CGameLevels GameLevels = (CGameLevels)obj;
            string sql = "DELETE FROM tblGameLevels";
            sql += " WHERE GameLevelsId =" + GameLevels.gameLevelId + "";
            //sql +=" AND CompanyId =" + GameLevels.CompanyId + "";
            return p.ExecuteNonQuery(sql);
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong tren CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(CLogin Login, IProvider p, CCollection Collection)
        {
            string sql = "SELECT * FROM tblGameLevels ORDER BY GameLevelsName";
            DataTable table = p.OpenTable(sql, "tblGameLevels");
            if (table != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    CGameLevels o = new CGameLevels(Login, p, row);
                    Collection.Add(o);
                }
            }
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong con cua doi obj neu co (danh cho loai du lieu quan he 2 bang cha con)
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(IProvider p, Class obj, CCollection Collection)
        {
            
        }
    }
    #endregion
}
